//
//  CheckInviterView.m
//  Rabbit
//
//  Created by 阿帕 on 2018/12/4.
//  Copyright © 2018 GCY. All rights reserved.
//

#import "CheckInviterView.h"

@interface CheckInviterView ()

@property(strong, nonatomic)UILabel *titleLabel;

@property(strong, nonatomic)UILabel *msgLabel;

@end

@implementation CheckInviterView

+ (CheckInviterView *)shared {
    
    static dispatch_once_t predicate;
    
    static CheckInviterView *objc;
    
    dispatch_once(&predicate, ^{
        
        objc = [[CheckInviterView alloc] init];
        
    });
    
    return objc;
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUpUI];
    }
    
    return self;
}

- (void)setUpUI {
    
    _title = @"警告";
    
    _message = @"确定要执行此操作吗？";
    
    self.frame = CGRectMake(0, 0, ScreenW, ScreenH);
        
    //黑色遮挡层
    UIView *barrierView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];
    barrierView.backgroundColor = RGBA(0, 0, 0, 0.5);
    [self addSubview:barrierView];
    
    //确认信息底层白色View
    UIView *confirView = [[UIView alloc] initWithFrame:CGRectMake(50, 0, ScreenW-100, 260)];
    confirView.cy_centerX = self.cy_centerX;
    confirView.cy_centerY = self.cy_centerY-50;
    confirView.backgroundColor = RGB(255, 255, 255);
    confirView.userInteractionEnabled = YES;
    [DCSpeedy dc_chageControlCircularWith:confirView AndSetCornerRadius:5 SetBorderWidth:0 SetBorderColor:nil canMasksToBounds:YES];
    [self addSubview:confirView];
    
    UIImageView *tipImage = [[UIImageView alloc] initWithFrame:CGRectMake((ScreenW-100)/2-25, 40, 50, 50)];
    tipImage.image = image(@"gcy_img_alert_lingdang");
    [confirView addSubview:tipImage];
    
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, tipImage.cy_bottom + 10, confirView.width, 30)];
    _titleLabel.text = self.title;
    _titleLabel.textColor = [UIColor cy_colorWithHexString:@"#434C67"];
    _titleLabel.font = font(18);
    _titleLabel.textAlignment= NSTextAlignmentCenter;
    [confirView addSubview:_titleLabel];
    
    _msgLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, _titleLabel.cy_bottom, confirView.width-20, 40)];
    _msgLabel.text = self.message;
    _msgLabel.textColor = [UIColor cy_colorWithHexString:@"#B6BED2"];
    _msgLabel.font = font(16);
    _msgLabel.numberOfLines = 0;
    _msgLabel.adjustsFontSizeToFitWidth = YES;
    _msgLabel.textAlignment = NSTextAlignmentCenter;
    [confirView addSubview:_msgLabel];
    
    NSArray *titles = @[@"取消", @"确定"];
    
    for (int i=0; i<2; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i + 300;
        [button setTitle:titles[i] forState:UIControlStateNormal];
        button.titleLabel.font = font(16);
        [button setTitleColor:i==0?Theme_ColorValue:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setBackgroundColor:i==0?[UIColor whiteColor]:Theme_ColorValue];
        button.frame = i==0 ? CGRectMake(35, _msgLabel.cy_bottom + 20, 90, 34) : CGRectMake(confirView.cy_width - 90 - 35, _msgLabel.cy_bottom + 20, 90, 34);
        cut(button, BTN_BORDER_RADIUS);
        if (i==0) {
            cutBorder(button, 1, @"#2C7CF6")
        }
        [button addTarget:self action:@selector(btnEventClick:) forControlEvents:UIControlEventTouchUpInside];
        [confirView addSubview:button];
    }
    
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 1.0;
    }];
    
}

- (void)btnEventClick:(UIButton *)button {
    
    if (button.tag == 301) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(CYNormalAlertViewBtnClick:)]) {
            [self.delegate CYNormalAlertViewBtnClick:button.tag];
        }
        
    } else {
     
        [UIView animateWithDuration:0.5 animations:^{
            self.alpha = 0.0;
        }];
    }
    
}

- (void)show {
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    _titleLabel.text = _title;
    _msgLabel.text = _message;
    
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 1.0;
    }];
}

- (void)hidden {
    
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0.0;
    }];
}

@end
